package com.pkutech.carcasher.network;

import android.text.TextUtils;

import com.pkutech.carcasher.bean.AlipayReqsponse;
import com.pkutech.carcasher.bean.BaseResponse;
import com.pkutech.carcasher.bean.BookParking;
import com.pkutech.carcasher.bean.BookParkingResponse;
import com.pkutech.carcasher.bean.GetInvoceInfoResponse;
import com.pkutech.carcasher.bean.GetRoadListResponse;
import com.pkutech.carcasher.bean.HmSelectFormalOrderResponse;
import com.pkutech.carcasher.bean.IllegalParkResponse;
import com.pkutech.carcasher.bean.IllegarllyParkLogResponse;
import com.pkutech.carcasher.bean.LoginResponse;
import com.pkutech.carcasher.bean.ParkingLogResponse;
import com.pkutech.carcasher.bean.ParkingLotResponse;
import com.pkutech.carcasher.bean.PayFeesResponse;
import com.pkutech.carcasher.bean.PrintRequest;
import com.pkutech.carcasher.bean.RegisterInfo;
import com.pkutech.carcasher.bean.RegisterResponse;
import com.pkutech.carcasher.bean.SelectBookingOrderResponse;
import com.pkutech.carcasher.bean.SelectFormalOrderResponse;
import com.pkutech.carcasher.bean.SysMsgResponse;
import com.pkutech.carcasher.db.RealmManager;
import com.pkutech.carcasher.utils.GsonTools;

import java.util.LinkedHashMap;
import java.util.Map;

import rx.Observable;
/**
 * Created by cyy on 2016/6/14.
 */
public class SoapApi{

    /**
     * 登陆
     * @param userName
     * @param password
     * @return
     */
    public static Observable<LoginResponse> login(final String userName, final String password) {
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("code", userName);
            map.put("password", password);
            String json = SoapClient.request("hmLogin", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, LoginResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * 注册
     * @param registerInfo
     * @return
     */
    public static Observable<RegisterResponse> register(RegisterInfo registerInfo){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("jsonMember", GsonTools.beanToJson(registerInfo));
            String json = SoapClient.request("appRegister", map);
            if(json != null){
                RegisterResponse registerResponse = new RegisterResponse();
                registerResponse.setMessage(json);
                subscriber.onNext(registerResponse);
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * 搜索车位
     * @return
     */
    public static Observable<ParkingLotResponse> appParkingLot(String name, String electricize, String ifDiscount){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("name", name);
            map.put("electricize", electricize);
            map.put("ifDiscount", ifDiscount);
            String json = SoapClient.request("appParkingLot", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, ParkingLotResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * 预约车位
     * @param bookParking
     * @return
     */
    public static Observable<BookParkingResponse> appBookParking(BookParking bookParking){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("json", GsonTools.beanToJson(bookParking));
            String json = SoapClient.request("appBookParking", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, BookParkingResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * 违停查询
     * @return
     */
    public static Observable<IllegarllyParkLogResponse> appIllegarllyParkLog(String account, String date){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("employeeCode", account);
            map.put("date", date);
            String json = SoapClient.request("hmGetBlackList", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, IllegarllyParkLogResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * 历史停车记录
     * @param memberAccount
     * @return
     */
    public static Observable<ParkingLogResponse> appParkingLog(String memberAccount){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("memberAccount", memberAccount);
            String json = SoapClient.request("appSelectFormalorder", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, ParkingLogResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * 预定订单查询
     * @param account
     * @return
     */
    public static Observable<SelectBookingOrderResponse> appSelectBookingOrder(String account, String date){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("code", account);
            if(!TextUtils.isEmpty(date)){
                map.put("date", date);
            }
            String json = SoapClient.request("hmSelectBookingOrder", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, SelectBookingOrderResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * app获取正式订单
     * @param account
     * @return
     */
    public static Observable<SelectFormalOrderResponse> appSelectFormalOrder(final String account, String date){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("account", account);
            if(!TextUtils.isEmpty(date)){
                map.put("date", date);
            }
            String json = SoapClient.request("appSelectFormalorder", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, SelectFormalOrderResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * hm 获取正式订单(停靠订单)
     * @param account
     * @return
     */
    public static Observable<HmSelectFormalOrderResponse> hmSelectFormalOrder(String account, String date){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("employeeCode", account);
            if(!TextUtils.isEmpty(date)){
                map.put("date", date);
            }
            String json = SoapClient.request("hmSelectFormalorder", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, HmSelectFormalOrderResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * 查询停车场
     * @return
     */
    public static Observable<GetRoadListResponse> appGetRoadList(){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            String json = SoapClient.request("appGetRoadList", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, GetRoadListResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * 预约确认
     * @return
     */
    public static Observable<BaseResponse> confirm(String orderId){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("orderCode", orderId);
            map.put("states", "true");
            String json = SoapClient.request("hmFeedBack", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, BaseResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * 预约确认
     * @return
     */
    public static Observable<BaseResponse> back(String orderId){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("orderCode", orderId);
            map.put("states", "false");
            String json = SoapClient.request("hmFeedBack", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, BaseResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }



    /**
     * 新增停靠
     * @return
     */
    public static Observable<BaseResponse> stopUpdate(String carNum,String ordercode, String carModel){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("code", ordercode);
            map.put("carNum", carNum);
            map.put("carPhoto","" );
            map.put("employeeCode",  RealmManager.findUserInfo().getCode());// RealmManager.findUserInfo().getRoadid()
            map.put("carModel", carModel);

            String json = SoapClient.request("hmParkingIn", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, BaseResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * 新增停靠
     * @return
     */
    public static Observable<BaseResponse> stopAdd(String carNum,String carportCode, String carModel){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("carportCode", carportCode);
            map.put("carNum", carNum);
            map.put("employeeCode",  RealmManager.findUserInfo().getCode());// RealmManager.findUserInfo().getRoadid()
            map.put("carPhoto","" );
            map.put("carModel", carModel);

            String json = SoapClient.request("hmAddFormalOrder", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, BaseResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * 付费出场
     * @return
     */
    public static Observable<BaseResponse> hmParkingOut(String code,String money,String employeeCode, int payType){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("code", code);
            map.put("money", money);
            map.put("employeeCode", employeeCode);
            map.put("payType", String.valueOf(payType));


            String json = SoapClient.request("hmParkingOut", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, BaseResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }
    /**
     * app客户缴费
     * @return
     */
    public static Observable<BaseResponse> appPayFees(String orderid,String moneyCount){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("orderCode", orderid);
            map.put("money", moneyCount);
            map.put("payType", "1");
            String json = SoapClient.request("appSettlement", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, BaseResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * hm获取缴费信息
     * @return
     */
    public static Observable<PayFeesResponse> getHmPayCondition(String orderId){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("code", orderId);
//            map.put("carModel", "1");//liuyong
            String json = SoapClient.request("hmParkingRate", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, PayFeesResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }
    /**
     * 违停申报
     * @return
     */
    public static Observable<IllegalParkResponse> hmIllegallyPark(String carNum, String carPhoto1, String carPhoto2, String carPhoto3, String employeeCode){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("carNum", carNum);
            map.put("carPhoto1", carPhoto1);
            map.put("carPhoto2", carPhoto2);
            map.put("carPhoto3", carPhoto3);
            map.put("employeeCode", employeeCode);

            String json = SoapClient.request("hmIllegallyPark", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, IllegalParkResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }
    /**
     * 27.HM员工登录手持端修改员工坐标
     * @return
     */
    public static Observable<BaseResponse> updateCoordinateInfoById(String lat,String lng){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("type", "login");
            map.put("id", RealmManager.findUserInfo().getId());
            map.put("lng", lng);
            map.put("lat", lat);//liuyong
            String json = SoapClient.request("updateCoordinateInfoById", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, BaseResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }
    public static Observable<BaseResponse> updateCoordinateInfoById2(String lat,String lng,String userid){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("type", "login");
            map.put("id", userid);
            map.put("lng", lng);
            map.put("lat", lat);//liuyong
            String json = SoapClient.request("updateCoordinateInfoById", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, BaseResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }
    /**
     * 28 HM手持机打印票务记录
     * @return
     */
    public static Observable<BaseResponse> hmInsertInvoceInfo(PrintRequest printRequest){
        return Observable.create(subscriber -> {
            String printInfojson= GsonTools.beanToJson(printRequest);
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("insertInfo", printInfojson);
            String json = SoapClient.request("hmInsertInvoceInfo", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, BaseResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    /**
     * 29 支付宝第三方接口调用
     * @return
     */
    public static Observable<AlipayReqsponse> alipyGetWay(String authCode,String money,String code){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("authCode", authCode);
            map.put("money", money);
            map.put("code", code);

            String json = SoapClient.request("AlipyGetWay", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, AlipayReqsponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }


    public static Observable<SysMsgResponse> getSysMsg(String logo, String date){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("logo", logo);
            map.put("date", date);
            String json = SoapClient.request("getMessage", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, SysMsgResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    public static Observable<GetInvoceInfoResponse> getInvoceInfo(String code){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("code", code);
            String json = SoapClient.request("getInvoceInfo", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, GetInvoceInfoResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }

    public static Observable<BaseResponse> hmLogout(String account){
        return Observable.create(subscriber -> {
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("code",account);
            String json = SoapClient.request("hmLogout", map);
            if(json != null){
                subscriber.onNext(GsonTools.getPerson(json, BaseResponse.class));
            }else{
                subscriber.onNext(null);
            }
        });
    }
}
